//
//  CommentHeaderViewFrame.m
//  XiaoHongShu
//
//  Created by Jason on 2022/8/16.
//

#import "CommentHeaderViewFrame.h"
#import "CommentListModel.h"
#import <YYText/YYText.h>


@implementation CommentHeaderViewFrame

-(void)setCommentModel:(CommentListModel *)commentModel {
    _commentModel = commentModel;
    [self setupFrame];
}

//设置控件的frame 和行高
- (void)setupFrame {
   //设置用户头像frame
    CGFloat userImageBtnX = 10;
    CGFloat userImageBtnY = 10;
    CGFloat userImageBtnW = 30;
    CGFloat userImageBtnH = 30;
    _userImageBtnFrame = CGRectMake(userImageBtnX, userImageBtnY, userImageBtnW, userImageBtnH);
    
    //用户名
    CGFloat userNameX = CGRectGetMaxX(_userImageBtnFrame) + 5;
    CGFloat userNameY = 10;
    CGFloat userNameH = 15;
    CGFloat userNameW = [_commentModel.userName boundingRectWithSize:CGSizeMake(MAXFLOAT, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: kFont(12)} context:nil].size.width;
    _userNameBtnFrame = CGRectMake(userNameX, userNameY, userNameW, userNameH);
    
    //是否是作者的评论
    CGFloat isAuthorLabelX = CGRectGetMaxX(_userNameBtnFrame) + 8;
    CGFloat isAuthorLabelY = userNameY;
    CGFloat isAuthorLabelH = 15;
    CGFloat isAuthorLabelW = 28;
    _isAuthorLabelFrame = CGRectMake(isAuthorLabelX, isAuthorLabelY, isAuthorLabelW, isAuthorLabelH);
    
    //评论点赞
    CGFloat likeBtnW = 30;
    CGFloat likeBtnH = 40;
    CGFloat likeBtnY = 10;
    CGFloat likeBtnX = kSPScreenWidth - likeBtnW - 10;
    _likeBtnFrame = CGRectMake(likeBtnX, likeBtnY, likeBtnW, likeBtnH);
    
    //评论文本
    //获取文本
    NSString *commentText = [NSString stringWithFormat:@"%@\n%@ %@", _commentModel.content, _commentModel.lastModifiedAt, _commentModel.ipLocation];
    CGFloat commentTextX = userNameX;
    CGFloat commentTextY = CGRectGetMaxY(_userNameBtnFrame) + 5;
    CGFloat commentTextW = kSPScreenWidth - 100;
   // CGFloat commentTextH = [self returnTextHeightWithStr:commentText withFont:kFont(14) WithWidth:commentTextW WithLineSpacing:0];
   // CGFloat commentTextH = [commentText boundingRectWithSize:CGSizeMake(commentTextW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: kFont(14)} context:nil].size.height;
   // CGFloat commentTextH =  [self getHeight:commentText width:commentTextW font:kFont(14)];
    
    
    NSMutableAttributedString *commentTextAttr = [[NSMutableAttributedString alloc]initWithString:commentText];
   // self.textLabel.attributedText = commentTextAttr;
    YYTextContainer *titleContarer = [YYTextContainer new];
    //限制宽度
    titleContarer.size = CGSizeMake(commentTextW, CGFLOAT_MAX);
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:commentTextAttr];
    CGFloat commentTextH = titleLayout.textBoundingSize.height;
    _commentTextLabelFrame = CGRectMake(commentTextX, commentTextY, commentTextW, commentTextH);
    _headerViewHeight = CGRectGetMaxY(_commentTextLabelFrame) + 10;
    
    
}

//获取自适应高度
- (CGFloat) getHeight:(NSString *)content width:(CGFloat)width font:(UIFont *)font {
    //UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    YYLabel *contentLabel = [[YYLabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    contentLabel.text = content;
    contentLabel.font = font;
    contentLabel.numberOfLines = 0;
    [contentLabel sizeToFit];
    return ceil(contentLabel.frame.size.height) + 2;
}


- (CGFloat )returnTextHeightWithStr:(NSString *)str withFont:(UIFont *)font WithWidth:(CGFloat)width WithLineSpacing:(CGFloat)lineSpacing  {
    UITextView* _textView = [[UITextView alloc]init];
    _textView.font = font;
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = lineSpacing;
    NSMutableAttributedString *titleAttr = [[NSMutableAttributedString alloc] initWithString:str];
    [titleAttr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, titleAttr.length)];
    [titleAttr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, titleAttr.length)];
    _textView.attributedText = titleAttr;
    CGSize maxSize = CGSizeMake(width,CGFLOAT_MAX);
    CGSize newSize = [_textView sizeThatFits:maxSize];
    CGFloat height = ceilf(newSize.height);
    return height;
}

@end
